package com.systemdevelopment.mainproject.Model;

import java.sql.*;
import java.util.HashSet;

public class Admin extends NormalUser {
    static final String Authority="ADMIN";//权限

    public String getAuthority() {
        return Authority;
    }

    //更改admin信息（更改密码）
    private static void UpdateAdminPasswordByAccount(String Account, String new_password) throws SQLException {
        Connection conn = DataBaseManager.build_conn() ;
        PreparedStatement preSql;

        String sqlStr = "update LIB.ADMINISTRATOR set PASSWORD = ? where ACCOUNT = ?";

        try {
            preSql = conn.prepareStatement(sqlStr);
            preSql.setString(1,new_password);
            preSql.setString(2,Account);

            preSql.executeUpdate();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //更改admin信息（更改权限）
    private static void UpdateAdminAuthorityByAccount(String Account, String new_authority) throws SQLException {
        Connection conn = DataBaseManager.build_conn() ;
        PreparedStatement preSql;

        String sqlStr = "update LIB.ADMINISTRATOR set AUTHORITY = ? where ACCOUNT = ?";

        try {
            preSql = conn.prepareStatement(sqlStr);
            preSql.setString(1,new_authority);
            preSql.setString(2,Account);

            preSql.executeUpdate();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //从数据库administrator和account_manager表中删除一行，参数是ACCOUNT
    public static void DeleteAdminFromDB(String Account) throws SQLException {
        Connection conn = null;
        try {
            conn = DataBaseManager.build_conn();
            String sql = "DELETE FROM LIB.ADMINISTRATOR WHERE ACCOUNT = '" + Account +"'";
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);

            //删除account_manager中账户
            DeleteAccountFromDB(Account);

        }catch (SQLException Se){
            Se.printStackTrace();
        }finally {
            conn.close();
        }
    }


    //从数据库administrator表中删除一行
    public static void DeleteAdminFromADMINISTRATOR(String Account) throws SQLException {
        Connection conn = null;
        try {
            conn = DataBaseManager.build_conn();
            String sql = "DELETE FROM LIB.ADMINISTRATOR WHERE ACCOUNT = '" + Account +"'";
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);

        }catch (SQLException Se){
            Se.printStackTrace();
        }finally {
            conn.close();
        }
    }


    //查询 根据account从数据库administrator表中检索用户
    public static Admin GetAdminFromDBByAccount(String Account) {
        Admin admin = new Admin();
        try {
            Connection conn = DataBaseManager.build_conn();
            String Sql = "select * from LIB.ADMINISTRATOR where ACCOUNT = " + "'" + Account + "'";

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(Sql);

            if (rs.next()) {
                String account = rs.getString("ACCOUNT");
                String password = rs.getString("PASSWORD");
                admin.setAccount(account);
                admin.setPassword(password);
                return admin;

            } else{
                //没找到此账户，异常处理
                return null;
            }
        }catch (SQLException Se) {
            Se.printStackTrace();
        }
        return null;

    }

    //获取所有Admin
    public static HashSet<Admin> GetAllAdminFromDB() {
        HashSet<Admin> AdminSet = new HashSet<>();

        try {
            Connection conn = DataBaseManager.build_conn();
            String Sql = "select * from LIB.ADMINISTRATOR";

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(Sql);

            while (rs.next()) {
                String account = rs.getString("ACCOUNT");
                String password = rs.getString("PASSWORD");
                String authority = rs.getString("AUTHORITY");

                if (account.length() > 0 && password.length() > 0 && authority.length() > 0) {
                    Admin admin = new Admin();
                    admin.setAccount(account);
                    admin.setPassword(password);
                    AdminSet.add(admin);
                }
            }
        }catch (SQLException Se) {
            Se.printStackTrace();
        }
        return AdminSet;
    }


    //向数据库的administrator表中插入多行，待插入的admin存储在HashSetMap<admin>中
    public static void InsertAdminIntoDB(HashSet<Admin> AdminSet){
        try{
            Connection conn = DataBaseManager.build_conn();
            Statement stmt = conn.createStatement();

            for (Admin admin:AdminSet){
                String account = admin.getAccount();
                String password = admin.getPassword();
                String authority = admin.getAuthority();

                String Sql = "Insert into LIB.ADMINISTRATOR VALUES("+ "'"+ account + "'" + "," + "'" + password + "'" + "," + "'" + authority + "'" + ")";

                stmt.addBatch(Sql);
            }

            int[] num = stmt.executeBatch();
        }catch (SQLException Se){
            Se.printStackTrace();
        }
    }


    //从数据库account_manager表中删除一行，参数是ACCOUNT
    public static void DeleteAccountFromDB(String Account) throws SQLException {
        try (Connection conn = DataBaseManager.build_conn()) {
            String sql = "DELETE FROM LIB.ACCOUNT_MANAGER WHERE ACCOUNT = '" + Account + "'";
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException Se) {
            Se.printStackTrace();
        }
    }




}
